/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System.IO;
using System.Diagnostics;
using DC;
using DC.Http;


namespace MediaDC
{
    static class PreviewManager
    {
        private const char EXT_SEPARATOR = ';';

        public static void RunPreview(DcFile file, string nickName, int hubConnectionID)
        {
            PreviewAssociation association = GetPreviewAssociation(file.Name);

            if (association != null && association.PreviewMode == PreviewAssociation.Mode.Stream)
            {
                Process.Start(association.Application, GetPreviewUrl(nickName, hubConnectionID, file.TTH));
            }
            else
            {
                HubClient c = HubConnectionManager.GetConnectedClient(hubConnectionID);
                if (c != null)
                    c.RequestFile(nickName, file.Name, file.TTH);
            }
        }

        public static void ProcessRecivedFile(string fileName)
        {
            PreviewAssociation association = GetPreviewAssociation(fileName);
            Process proc = new Process();

            if (association == null || string.IsNullOrEmpty(association.Application))
            {
                proc.StartInfo.FileName = fileName;
                proc.StartInfo.UseShellExecute = true;
            }
            else
            {
                proc.StartInfo.FileName = association.Application;
                proc.StartInfo.Arguments = fileName;
            }
            
            proc.Start();
        }


        public static string GetPreviewUrl(string nickName, int hubConnectionID, string TTH)
        {
            return string.Format("http://127.0.0.1:{0}/{3}/{1}/TTH/{2}",
                                 Config.Instance.WebServerPort,
                                 nickName,
                                 TTH,
                                 hubConnectionID);
        }

        public static void RunMagnetPreview(DcFile file)
        {
            string mangetLink =
                string.Format("magnet:?xt=urn:tree:tiger:{0}&xl={1}&dn={2}", file.TTH, file.Size, HttpUtility.EncodeURL(file.Name));
            Process p = new Process();
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.FileName = mangetLink;
            p.Start();
        }

        private static PreviewAssociation GetPreviewAssociation(string fileName)
        {
            string fileExt = Path.GetExtension(fileName).ToLower();
            if (fileExt.StartsWith("."))
                fileExt = fileExt.Remove(0, 1);

            return Config.Instance.PreviewAssociations.Find(
                 (delegate(PreviewAssociation value)
                      {
                          foreach (string currExt in value.Exts.Trim().Split(EXT_SEPARATOR))
                          {
                              string ext = currExt.Trim().ToLower();
                              if (ext.StartsWith("."))
                                  ext = ext.Remove(0, 1);
                              if (ext == fileExt)
                                  return true;
                          }
                          return false;
                      }));
        }


    }
}
